package com.wind.adapter;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.bumptech.glide.Glide;
import com.wind.beans.DiaryItemBean;
import com.debug.soblogin.R;
import com.wind.soblogin.DebugApplication;

import java.util.ArrayList;
import java.util.List;

public class MoreTypeAdapter extends RecyclerView.Adapter<MoreTypeAdapter.InnerHolder> {

    private final List<DiaryItemBean> mData;
    //定义三种类型
    public static final int TYPE_NO_IMAGE = 0;
    public static final int TYPE_ONE_IMAGE = 1;
    public static final int TYPE_TWO_IMAGE = 2;
    public static final int TYPE_THREE_IMAGE = 3;

    public MoreTypeAdapter(ArrayList<DiaryItemBean> data) {
        this.mData = data;
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_NO_IMAGE) {

            view = View.inflate(parent.getContext(), R.layout.item_type_no_image, null);
            return new InnerHolder(view);

        } else if (viewType == TYPE_ONE_IMAGE) {

            view = View.inflate(parent.getContext(), R.layout.item_type_one_image, null);
            return new InnerHolder(view);

        } else if (viewType == TYPE_TWO_IMAGE) {
            view = View.inflate(parent.getContext(), R.layout.item_type_two_image, null);
            return new InnerHolder(view);
        } else {
            view = View.inflate(parent.getContext(), R.layout.item_type_three_image, null);
            return new InnerHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(mData.get(position), position);
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }


    @Override
    public int getItemViewType(int position) {
        DiaryItemBean db = mData.get(position);
        if (db.getImageNum() == 0) {
            return TYPE_NO_IMAGE;
        } else if (db.getImageNum() == 1) {
            return TYPE_ONE_IMAGE;
        } else if (db.getImageNum() == 2) {
            return TYPE_TWO_IMAGE;
        } else {
            return TYPE_THREE_IMAGE;
        }
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        private final TextView mDiaryContent;
        private final TextView mDiary_time;
        private final ImageView mImageOne;
        private final ImageView mImageTwo;
        private final ImageView mImageThree;
        private int mPosition;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);

            mDiaryContent = itemView.findViewById(R.id.diary_title);
            mImageOne = itemView.findViewById(R.id.image_01);
            mImageTwo = itemView.findViewById(R.id.image_02);
            mImageThree = itemView.findViewById(R.id.image_03);
            mDiary_time = itemView.findViewById(R.id.diary_time);
        }

        @SuppressLint("SetTextI18n")
        public void setData(DiaryItemBean diaryItemBean, int position) {
            this.mPosition = position;
            int imageNum = diaryItemBean.getImageNum();
            //TODO:这里的数据处理也有问题
            System.out.println("lklklkkl---->" + diaryItemBean.getDiaryContent());
            String diaryContent = diaryItemBean.getDiaryContent();
            String createTime = diaryItemBean.getCreateTime();


            String time_front = "0";
            String time_behind = "0";
//            @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
//            Date date = new Date(System.currentTimeMillis());
//            String currentTime = String.valueOf(date);
//            System.out.println(formatter.format(currentTime));
            String createTime_front = createTime.substring(5, 10);
            String minute = createTime.substring(13, 16);
            int hour = Integer.parseInt(createTime.substring(11, 13)) + 8;
//            //2018-11-25 at 01:22:12 CET
//            //计算是哪一天，如果实前天，昨天，今天要做特殊处理，
//            //其中0-4是年，5-7是月份，9-11是日期
//            String currentTime_year = currentTime.substring(0, 4);
//            String currentTime_month = currentTime.substring(5, 7);
//            String currentTime_day = currentTime.substring(9, 11);
//            System.out.println("当前时间---》" + currentTime_year + currentTime_month + currentTime_day);
//            String createTime_year = createTime.substring(0, 4);
//            String createTime_month = createTime.substring(5, 7);
//            String createTime_day = createTime.substring(9, 11);
//            System.out.println("创作时间---》" + createTime_year + createTime_month + createTime_day);
//            if (currentTime_year.equals(createTime_year)) {
//                if (currentTime_month.equals(createTime_month)) {
//                    if (currentTime_day.equals(createTime_day)) {
//                        time_front = "今天";
//                        time_behind = createTime.substring(11, 16);
//                    } else {
//                        time_front = createTime_front;
//                        time_behind = createTime_behind;
//                    }
//                } else {
//                    time_front = createTime_front;
//                    time_behind = createTime_behind;
//                }
//            } else {
//                time_front = createTime_front;
//                time_behind = createTime_behind;
//            }

            mDiary_time.setText(createTime_front + "  " + hour + minute);
            if (diaryContent != null) {
                mDiaryContent.setText(diaryContent);
            } else {
                mDiaryContent.setText("这里出现了问题");
            }

            if (imageNum == 1) {

                Glide.with(DebugApplication.getContext()).load(diaryItemBean.getPicList().get(0)).into(mImageOne);
            } else if (imageNum == 2) {
                Glide.with(DebugApplication.getContext()).load(diaryItemBean.getPicList().get(0)).into(mImageOne);
                Glide.with(DebugApplication.getContext()).load(diaryItemBean.getPicList().get(1)).into(mImageTwo);
            } else if (imageNum == 3) {
                Glide.with(DebugApplication.getContext()).load(diaryItemBean.getPicList().get(0)).into(mImageOne);
                Glide.with(DebugApplication.getContext()).load(diaryItemBean.getPicList().get(1)).into(mImageTwo);
                Glide.with(DebugApplication.getContext()).load(diaryItemBean.getPicList().get(2)).into(mImageThree);
            }


        }
    }

}


